Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
JavaScript Higher Order Function keeps returning newArray as [Nan, Nan, Nan] when it should be numbers

I've been working on a problem that involves higher order functions and callbacks. The callback function is below.

**const addTwo = num => num + 2;**

I'm trying to pass this callback into the higher order function below.

**const map = (array, callback) => {
  let newArray = [];
  for (let i = 0; i < array.length; i++) {
    newArray.push(callback(newArray[i]))
  }
  return newArray;
};**

The variable "newArray," inside of the map function should be returned as an array of 3 different numbers added by two. The arguments for the map function's parameters are below within a console log.

**console.log(map([1, 2, 3], addTwo));**

The addTwo function should become the argument of the callback parameter within the map function, this should make every element in the above array be added by two. I keep seeing [NaN, NaN, NaN] in the console and I'm not quite sure why every number in the index isn't being added by two.

I would appreciate the help.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are pushing the values from the new array, when it should be the argument instead

**const map = (array, callback) => {
  let newArray = [];
  for (let i = 0; i < array.length; i++) {
    newArray.push(callback(array[i]))
  }
  return newArray;
};**
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda